import 'package:lcapp/model/db/sql_constant.dart';
import 'package:lcapp/model/db/sqlite.dart';
import 'package:lcapp/tools/map_tool.dart';
import 'package:sqflite/sqflite.dart';

class ApplePayDBOperate {

  //创建充值记录
  static Future<void> insertPayHistoryInfo(Map data) async {
    Database database =
    await SqliteSingleton.getInstance().getUserDB();
    List results = await database.rawQuery(
        'SELECT * ' +
            'FROM  $TB_APPLE_PAY_HISTORY  ' +
            'WHERE order_id = ?',
        [data["orderId"],]);
    if(results!=null&&results.length>0){
      int results = await database.update(TB_APPLE_PAY_HISTORY, MapTool.camel2under(data),where: "order_id = ?", whereArgs: [data["orderId"]]);
    }else{
      int results = await database.insert(TB_APPLE_PAY_HISTORY, MapTool.camel2under(data));
    }
  }

  //获取充值订单信息
  static Future<Map> getOrderInfo(Map data) async {
    Map<String,dynamic> map={};
    Database database =
    await SqliteSingleton.getInstance().getUserDB();
    List results = await database.rawQuery(
        'SELECT * ' +
            'FROM  $TB_APPLE_PAY_HISTORY  ' +
            'WHERE product_id = ? ORDER BY "create_time" DESC',
        [data["productId"],]);
    print('orderInfo ===  ' + results.toString());
    if(results!=null&&results.length>0){
      map = results[0];
    }
    return MapTool.under2camel(map);
  }

  //删除订单充值记录
  static Future<void> deletePayHistory(int orderId) async {
    Database database =
    await SqliteSingleton.getInstance().getUserDB();
    int delResults = await database.rawDelete('DELETE FROM "$TB_APPLE_PAY_HISTORY" WHERE order_id = $orderId',);
  }

}

